#!/bin/bash
baseDirForScriptSelf=$(cd "$(dirname "$0")"; pwd)
# 代理账号配置,如不需要使用代理,请注释/忽略/不填
is_need_proxy_tmp=`curl -I -m 10 -o /dev/null -s -w %{http_code} gitee.com`
if [ "${is_need_proxy_tmp}" == "200" -o "${is_need_proxy_tmp}" == "301" -o "${is_need_proxy_tmp}" == "302" ];then
    is_need_proxy=false
else
    is_need_proxy=true
fi
# 从source.sh获取账号密码
source ${baseDirForScriptSelf}/source.sh


# 检查是否可以直接访问gitee,如果不可以需要有代理配置
if [ "X${is_need_proxy}" == "Xtrue" -a "X${proxy_user}" == "X" -a "X${URL_proxy_passwd}" == "X" ];then
    echo "经检测,当前系统无法直接访问gitee.com,请检查网络配置"
    echo "如果需要使用代理,请修改source.sh脚本"
    exit 1
elif [ "X${is_need_proxy}" == "Xfalse" ];then
    return 0
fi

# http/https代理
# 如果不需要账号密码,请删除${proxy_user}:${URL_proxy_passwd}@
export http_proxy=http://${proxy_user}:${URL_proxy_passwd}@${proxy_domain}:${proxy_port-80}
export https_proxy=http://${proxy_user}:${URL_proxy_passwd}@${proxy_domain}:${proxy_port-80}
export no_proxy=*.huawei.com

# git代理
git config --global http.https://gitee.com.proxy http://${proxy_user}:${URL_proxy_passwd}@${proxy_domain}:${proxy_port-80}
git config --global http.https://lfs.gitee.com.proxy http://${proxy_user}:${URL_proxy_passwd}@${proxy_domain}:${proxy_port-80}
git config --global http.https://openharmony.gitee.com.proxy http://${proxy_user}:${URL_proxy_passwd}@${proxy_domain}:${proxy_port-80}

# 替换curl 和wget,默认走代理
alias curl="curl -x http://${proxy_user}:${URL_proxy_passwd}@${proxy_domain}:${proxy_port-80}"
alias wget="wget -e \"https_proxy=http://${proxy_user}:${URL_proxy_passwd}@${proxy_domain}:${proxy_port-80}\""